package com.string.actortv.util;

import android.util.Log;
import com.string.actortv.dto.MovieInfo;
import com.string.actortv.dto.MovieList;
import com.string.actortv.dto.MovieTypeList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import static com.string.actortv.util.CacheHelper.CACHE;

public class IdyBeeUtil {

    private static final String BASE_PAGE_URL = "https://www.idybee.com/page/";
    private static final String BASE_TYPE_URL = "https://www.idybee.com/tv";

    public interface MovieTypeCallBack {
        void execute(List<MovieTypeList> movieTypeList);
    }

    public static void getMovieTypeAsync(MovieTypeCallBack callBack) {
        MyHttpUtil.getAsync(BASE_TYPE_URL, html -> {
            Document document = Jsoup.parse(html);
            List<MovieTypeList> movieTypeLists = new ArrayList<>();
            Elements aElements = document.getElementsByClass("fl_list").get(1)
                    .getElementsByTag("a");
            for (Element a : aElements) {
                MovieTypeList movieTypeList;
                if ("全部".equals(a.text())) {
                    movieTypeList = new MovieTypeList(a.text(), BASE_PAGE_URL);
                } else {
                    movieTypeList = new MovieTypeList(a.text(), a.attr("href") + "/page/");
                }
                movieTypeLists.add(movieTypeList);
            }
            callBack.execute(movieTypeLists);
        }, null);
    }

    public interface MovieListCallBack {
        void execute(List<MovieList> movieList);
    }

    public static void getMovieListAsync(int pageNo, String searchStr,
                                         String url, MovieListCallBack movieListCallBack) {
        String pageUrl = url + pageNo + "?s=" + (searchStr == null ? "" : searchStr);
        if (CACHE.containsKey(pageUrl)
                && CACHE.get(pageUrl) != null) {
            getList(pageUrl, CACHE.get(pageUrl).toString(), movieListCallBack);
        } else {
            MyHttpUtil.getAsync(pageUrl, result -> {
                getList(pageUrl, result, movieListCallBack);
            }, null);
        }
    }

    private static void getList(String pageUrl,
                                String html,
                                MovieListCallBack movieListCallBack) {
        if ("".equals(html)) {
            movieListCallBack.execute(new ArrayList<>());
            return;
        }
        CACHE.put(pageUrl, html);
        Document document = Jsoup.parse(html);
        List<MovieList> movieList = new ArrayList<>();
        //获取当前电影列表
        Elements movieElements = document
                .getElementsByClass("i_list");
        if (movieElements != null) {
            for (Element movieElement : movieElements) {
                Elements elements = movieElement.getElementsByTag("a");
                if (!elements.isEmpty()) {
                    String name = elements.get(0).attr("title");
                    String img = elements.get(0).getElementsByTag("img").attr("data-original");
                    String url = elements.get(0).attr("href");
                    movieList.add(new MovieList(name, url, img));
                }
            }
        }
        movieListCallBack.execute(movieList);
    }

    public interface MovieInfoCallBack {
        void execute(MovieInfo movieInfo);
    }

    public static void getMovieInfoAsync(String href, String name, MovieInfoCallBack movieInfoCallBack) {
        //获取电影集数列表
        if (CACHE.containsKey(href)
                && CACHE.get(href) != null) {

            getInfo(name, CACHE.get(href).toString(), movieInfoCallBack);
        } else {
            MyHttpUtil.getAsync(href, movieHtml -> {
                if ("".equals(movieHtml)) {
                    movieInfoCallBack.execute(null);
                    return;
                }
                CACHE.put(href, movieHtml);

                getInfo(name, movieHtml, movieInfoCallBack);
            }, null);
        }
    }

    private static void getInfo(String name, String movieHtml, MovieInfoCallBack movieInfoCallBack) {
        Document document = Jsoup.parse(movieHtml);
        Elements descElements = document.getElementsByClass("movie-introduce");
        String desc = "";
        if (!descElements.isEmpty()) {
            desc = descElements.get(0).text();
        }
        Elements elements = document
                .getElementsByClass("page_ullist");
        List<MovieInfo.SeasonList> seasonList = new ArrayList<>();
        if (!elements.isEmpty()) {
            Elements listElements = elements.get(0).getElementsByTag("li");
            for (Element seasonElement : listElements) {
                String seasonName = seasonElement.getElementsByTag("a").get(0).text();
                String url = seasonElement.getElementsByTag("a").get(0).attr("href");

                seasonList.add(new MovieInfo.SeasonList(seasonName, url));
            }
        }
        movieInfoCallBack.execute(new MovieInfo(name, "", desc, seasonList));
    }

    public interface MovieUrlCallBack {
        void execute(String playUrl);
    }

    public static void getMovieUrlAsync(String pageUrl, MovieUrlCallBack callBack) {
        MyHttpUtil.getAsync(pageUrl, html -> {
            try {
                Document document = Jsoup.parse(html);
                Element iframeElement = document.getElementsByTag("iframe").get(0);
                String playUrl = iframeElement.attr("src").split("=")[1];
                Log.d("playUrl", playUrl);
                callBack.execute(playUrl);
            } catch (Exception e) {
                callBack.execute("");
            }
        }, null);
    }
}
